﻿using System.Text;
using Microsoft.SharePoint;
using System.Web.UI;
using VPLink2013.Framework;
using VPLink2013.Framework.Constants;
using VPLink2013.Framework.Extensions;
using System;

namespace VPLink2013.Webcontrols
{
    public class Drapeau: WebcontrolVPLink
    {
        protected override void Render(HtmlTextWriter writer)
        {
            Template = "webcontrol Drapeau";
            try
            {
                if (!string.IsNullOrEmpty(siteUrl))
                {
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        using (SPWeb web = site.RootWeb)
                        {
                            SPList lst = web.Lists.TryGetList(ListTitles.LiensInternationalTitle);
                            if (lst != null)
                            {
                                ProcessDrapeau(lst);
                            }
                        }
                    }
                }
            }
            catch (System.Exception exc)
            {
                writer.Write("webcontrol Drapeau.Erreur.Render : " + exc.Message + "/" + exc.StackTrace);
                ContextTrace.AddTrace(string.Format("Exception lors de l'affichage Drapeau : {0}", exc.Message));
                ContextTrace.AddTrace(string.Format("{0}", exc.StackTrace));
            }
            base.Render(writer);
        }

        private void ProcessDrapeau(SPList lst)
        {
            SPQuery spQuery = new SPQuery();
            StringBuilder sb = new StringBuilder();
            sb.Append("<Where>");
            sb.Append("<Eq><FieldRef Name='Visible'/><Value Type='Text'>Oui</Value></Eq>");
            sb.Append("</Where>");
            sb.Append("<OrderBy><FieldRef Name='Ordre' Ascending='TRUE'/><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>");
            string Query = sb.ToString();
            spQuery.Query = Query;
            ContextTrace.AddTrace("spQuery.Query : " + spQuery.Query);

            SPListItemCollection items = lst.GetItems(spQuery);

            sbHtml.AppendLine("<div id=\"langageLine\" style=\"float:right;\">");
            foreach (SPListItem item in items)
            {
                try
                {
                    CreateDrapeauItem(item);
                }
                catch (Exception ex)
                {
                    ContextTrace.AddTrace(string.Format("Exception lors de creation d'un item drapeau : {0}", ex.Message));
                    ContextTrace.AddTrace(string.Format("{0}", ex.StackTrace));
                }
            }
            sbHtml.AppendLine("</div>");
        }

        private void CreateDrapeauItem(SPListItem item)
        {
            string strLinkURL = item.GetItemListFieldValueAsUrlValue(Fields.UrlNewsField); ;
            string strTexteURL = item.GetItemListFieldValueAsUrlDescription(Fields.UrlNewsField);
            string strTextePiwik = strTexteURL.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");

            ContextTrace.AddTrace("strTexteURL : " + strTexteURL);
            ContextTrace.AddTrace("strLinkURL : " + strLinkURL);

            string strLinkImage = item.GetItemListFieldValueAsUrlValue(Fields.ImageField);
            string strTexteImage = item.GetItemListFieldValueAsUrlDescription(Fields.ImageField);


            ContextTrace.AddTrace("strLinkImage : " + strLinkImage);
            ContextTrace.AddTrace("strTexteImage : " + strTexteImage);
            strLinkImage = "<img src=\"" + strLinkImage + "\" alt=\"" + strTexteImage + "\">";

            string strURL = "<a href=\"" + strLinkURL + "\" onclick=\"javascript:_paq.push(['trackPageView', 'Drapeau/" + strTextePiwik + "']);\" target=\"_new\">" + strLinkImage + "</a>";
            ContextTrace.AddTrace("strURL : " + strURL);
            sbHtml.AppendLine(strURL);
        }
    }
}
